package com.lnu_devteam.zongce.controller;

import com.lnu_devteam.zongce.annotation.AuthCheck;
import com.lnu_devteam.zongce.common.BaseResponse;
import com.lnu_devteam.zongce.common.ErrorCode;
import com.lnu_devteam.zongce.common.ResultUtils;
import com.lnu_devteam.zongce.common.TokenResponse;
import com.lnu_devteam.zongce.constant.UserConstant;
import com.lnu_devteam.zongce.exception.BusinessException;
import com.lnu_devteam.zongce.model.dto.UserChangePwdDTO;
import com.lnu_devteam.zongce.model.dto.UserDTO;
import com.lnu_devteam.zongce.model.dto.UserLoginDTO;
import com.lnu_devteam.zongce.model.entity.User;
import com.lnu_devteam.zongce.model.enums.RoleEnum;
import com.lnu_devteam.zongce.model.vo.UserVO;
import com.lnu_devteam.zongce.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author xbaozi
 * @version 1.0
 * @classname UserController
 * @date 2023-03-23  15:25
 * @description 用户接口
 */
@Api(value = "/user", tags = {"用户接口"})
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    // region 登录相关接口

    /**
     * @description 用户登录接口
     * @author xbaozi
     * @date 2023/3/23 16:11
     * @param userLoginDTO  用户登录接口请求参数
     * @return BaseResponse<UserVO>
     **/
    @PostMapping("/login")
    @ApiOperation(value = "用户登录接口")
    public TokenResponse<UserVO> login(@RequestBody UserLoginDTO userLoginDTO, HttpServletRequest request) {
        if (userLoginDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return userService.login(userLoginDTO, request);
    }

    /**
     * @description 退出登录态
     * @author xbaozi
     * @date 2023/3/23 19:54
     * @return BaseResponse<Boolean>   是否成功
     **/
    @PostMapping("/logout")
    @ApiOperation(value = "退出登录态")
    public BaseResponse<Boolean> logout() {
        //request.getSession().removeAttribute(UserConstant.LOGIN_KEY);
        userService.removeLoginStatus();
        return ResultUtils.success(Boolean.TRUE);
    }

    // endregion

    // region 增删改查

    /**
     * @description 新增单个用户
     * @author xbaozi
     * @date 2023/3/23 20:03
     * @param userDTO  用户信息
     * @return BaseResponse<Long>   新增用户id
     **/
    @PostMapping("/add")
    @ApiOperation(value = "新增单个用户")
    @AuthCheck(anyRole = {RoleEnum.ADMIN, RoleEnum.SUPER})
    public BaseResponse<Long> add(@RequestBody UserDTO userDTO) {
        if (userDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long userId = userService.addUser(userDTO);
        return ResultUtils.success(userId);
    }

    @PostMapping("/add/upload")
    @ApiOperation(value = "批量添加用户")
    @AuthCheck(anyRole = {RoleEnum.ADMIN, RoleEnum.SUPER})
    public BaseResponse<Boolean> uploadToSaveBatch(@RequestBody MultipartFile file) {
        if (file == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = userService.upload(file);
        return ResultUtils.success(res);
    }

    /**
     * @description 获取当前用户信息
     * @author xbaozi
     * @date 2023/3/23 20:56
     * @return BaseResponse<UserVO> 脱敏后的用户信息
     **/
    @GetMapping("/current")
    @ApiOperation(value = "获取当前用户信息")
    public BaseResponse<UserVO> getCurrentUser(HttpServletRequest request) {
        UserVO userVO = new UserVO();
        User loginUser = userService.getLoginUser(request);
        BeanUtils.copyProperties(loginUser, userVO);
        return ResultUtils.success(userVO);
    }

    /**
     * @description 修改个人账号密码
     * @author xbaozi
     * @date 2023/3/23 22:18
     * @return BaseResponse<Boolean>    是否修改成功
     **/
    @PostMapping("/change/pwd")
    @ApiOperation(value = "修改个人账号密码")
    public BaseResponse<Boolean> changePwd(@RequestBody UserChangePwdDTO userChangePwdDTO, HttpServletRequest request) {
        if (userChangePwdDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = userService.changePwd(userChangePwdDTO, request);
        return ResultUtils.success(res);
    }

    @PostMapping("/reset/pwd")
    @ApiOperation(value = "重置指定用户密码")
    @AuthCheck(anyRole = {RoleEnum.ADMIN, RoleEnum.SUPER})
    public BaseResponse<Boolean> resetPwd(Long targetId, HttpServletRequest request) {
        if (targetId == null || targetId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = userService.resetPwd(targetId, request);
        return ResultUtils.success(res);
    }

    // endregion
}
